package com.gz.university.crm.bean;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;

import org.apache.commons.lang3.StringUtils;

import com.gz.university.crm.container.ServiceProvider;
import com.gz.university.crm.entity.SysMenuPrivilege;
import com.gz.university.crm.entity.SysUser;
import com.gz.university.crm.service.ISysMenuPrivilegeService;

/**
 * 自定义标签用户判断当前的菜单是否显示的问题
 * 
 * @author jack
 * 
 */
public class CheckMenuTag implements SimpleTag {
	private PageContext pageContext;
	// 标签体
	private JspFragment jspFragment;
	/*
	 * <mycrm:checkMenu module="${sysMenu.menuModule}"
	 * privilege="${sysMenu.menuPrivilege}">
	 */
	private String module;
	private String privilege;
	@Override
	public void doTag() throws JspException, IOException {
		// TODO Auto-generated method stub
		// 获取request对象
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		// 获取当前登录对象
		SysUser sysUser = SessionUtils.getSysUserFromSesion(request);
		if (sysUser == null) {
			return;
		}
		if (sysUser.getSysRole() == null) {
			return;
		}
		// 获取当前登录用户的Id
		Integer roleId = sysUser.getSysRole().getSysRoleId();

		// 获取菜单操作权限的业务层
		// 获取菜单操作权限表的所有数据，为了二级缓存,这里的依赖注入没法实现只能自己写容器然后得到spring的实体类
		ISysMenuPrivilegeService sysMenuPrivilegeService=(ISysMenuPrivilegeService) ServiceProvider.getService(ISysMenuPrivilegeService.SERVICE_NAME);
		List<SysMenuPrivilege> list =sysMenuPrivilegeService.findAll();

		if (list != null && list.size() > 0) {
			// 遍历集合，比对权限Id+模块名称+操作名称
			// 输出标签
			for (SysMenuPrivilege s : list) {
				if (roleId != null && StringUtils.isNotBlank(module)
						&& StringUtils.isNotBlank(privilege)) {
					if (roleId == s.getRoleId()
							&& module.equals(s.getMenuModule())
							&& privilege.equals(s.getMenuPrivilege())) {
						// 输出标签
						this.jspFragment.invoke(null);
						break;
					}
				}
			}

		}

	}

	@Override
	public void setParent(JspTag parent) {
		// TODO Auto-generated method stub

	}

	@Override
	public JspTag getParent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setJspContext(JspContext pc) {
		// TODO Auto-generated method stub
		this.pageContext = (PageContext) pc;
	}

	@Override
	public void setJspBody(JspFragment jspFragment) {
		// TODO Auto-generated method stub
		this.jspFragment = jspFragment;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getPrivilege() {
		return privilege;
	}

	public void setPrivilege(String privilege) {
		this.privilege = privilege;
	}

}
